package mainpackage;

import java.util.ArrayList;

public class BrentsAlgorithm {

	
	public static void main(String[] args) {
		
		System.out.println("Our sequence");
		int x0=8;
		int next=x0;
		for(int i=0;i<30;i++){
			System.out.print(next+",");
			next=f(next);
		}
		System.out.println();
		
		
		brent(x0);
		
		
	}
	
	public static void brent(int x0){
		System.out.println("Brent's Algorithm");
		
		int power =1 ;
		int lam=1;
		int tortoise=f(x0);
		int hare=f(f(x0));
		
		while(tortoise!=hare){
			if(power==lam){
				tortoise=hare;
				power=power*2;
				lam=0;
			}
			//System.err.println(hare);
		hare = f(hare);
		lam += 1;
		}
		
		
	
		
		
		
		 
		int mu=0;
		tortoise=hare=x0;
		for(int i=0;i<lam;i++){
			   hare = f(hare);
		}	   
			   while(tortoise != hare){
				   tortoise = f(tortoise);
				   hare = f(hare);
				   mu += 1;
			   }
		
		System.out.println("Result : mu="+mu+";"+"lam="+lam);
		System.out.println("Description: mu - is position when cycle begins , lam - is cycle lenght");
	}
	

	
	// primitive function - of functional graph  (look on image in project)
	public static int f(int x){
		if(x==0) return 6;
		if(x==1) return 6;
		if(x==2) return 0;
		if(x==3) return 1;
		if(x==4) return 4;
		if(x==5) return 3;
		if(x==6) return 3;
		if(x==7) return 4;
		if(x==8) return 0;
		return -1;
	}
	
	
}
